//
//  HLRtcMatchSuccessView.m
//  YueYue
//
//  Created by hulu001 on 2023/6/20.
//

#import "HLRtcMatchSuccessView.h"
#import <FrameAccessor/FrameAccessor.h>

@interface HLRtcMatchAvatarView : UIView
@property (strong,  nonatomic) UIImageView *avatarView;
@property (strong,  nonatomic) UILabel *nickLabel;
@property (strong,  nonatomic) QMUIButton *ageView; // 因为button具备背景图片+图片+文字 没有点击操作
@end

@implementation HLRtcMatchAvatarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        
        _avatarView = [UIImageView.alloc init];
        _avatarView.layer.cornerRadius = 134 / 2;
        _avatarView.layer.masksToBounds = YES;
        _avatarView.layer.borderColor = UIColor.whiteColor.CGColor;
        _avatarView.layer.borderWidth = 3;
        [self addSubview:_avatarView];
        [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(134);
            make.left.top.mas_equalTo(0);
        }];
        
        _nickLabel = [UILabel.alloc init];
        _nickLabel.textColor = UIColor.whiteColor;
        _nickLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:_nickLabel];
        [_nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.avatarView.mas_bottom).offset(12);
            make.centerX.equalTo(self);
            make.left.mas_greaterThanOrEqualTo(17);
            make.right.mas_lessThanOrEqualTo(-17);
        }];
        
        _ageView = [QMUIButton buttonWithType:UIButtonTypeCustom];
        _ageView.layer.cornerRadius = 8;
        _ageView.layer.masksToBounds = YES;
        [_ageView setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _ageView.titleLabel.font = [UIFont systemFontOfSize:10];
        _ageView.spacingBetweenImageAndTitle = 1;
        [self addSubview:_ageView];
        [_ageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(32, 16));
            make.bottom.mas_equalTo(0);
            make.centerX.equalTo(self);
        }];
    }
    return self;
}

@end


@interface HLRtcMatchSuccessView ()

@property (weak,    nonatomic) UIImageView *heartImageV;
@property (weak,    nonatomic) HLRtcMatchAvatarView *leftAvatar;

@end

@implementation HLRtcMatchSuccessView

- (instancetype)initWithFrame:(CGRect)frame type:(NSInteger)type
{
    self = [super initWithFrame:frame];
    if (self) {
        _type = type;
        
        if (type == 1) {
            _rtcView = [UIView.alloc initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame))];
            [self addSubview:_rtcView];
            
            _maskView = [UIView.alloc initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame))];
            _maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.8];
            [self addSubview:_maskView];
        }
        
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setImage:[UIImage imageNamed:@"match_back"] forState:UIControlStateNormal];
        [self addSubview:backBtn];
        [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.top.mas_equalTo(StatusBarHeightConstant);
            make.width.height.mas_equalTo(44);
        }];
        MJWeakSelf
        backBtn.qmui_tapBlock = ^(__kindof UIControl *sender) {
            if (weakSelf.backBlock) {
                weakSelf.backBlock();
            }
        };
        
        UIImageView *topImgV = [UIImageView.alloc init];
        topImgV.viewSize = CGSizeMake(220, 33);
        topImgV.centerX = self.middleX;
        topImgV.top = 150;
        topImgV.image = [UIImage imageNamed:@"match_result_top"];
        [self addSubview:topImgV];
        
        UILabel *resultLabel = [UILabel.alloc init];
        resultLabel.text = @"匹配成功!";
        resultLabel.font = [UIFont systemFontOfSize:24 weight:UIFontWeightMedium];
        resultLabel.textColor = UIColor.whiteColor;
        [topImgV addSubview:resultLabel];
        [resultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(topImgV);
        }];
        
        _statusLabel = [UILabel.alloc init];
        _statusLabel.textColor = UIColor.whiteColor;
        _statusLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:_statusLabel];
        [_statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(topImgV.mas_bottom).offset(12);
            make.centerX.equalTo(self);
        }];
        
        HLRtcMatchAvatarView *leftAvatarView = [HLRtcMatchAvatarView.alloc init];
        leftAvatarView.viewSize = CGSizeMake(134, 186);
        leftAvatarView.left = (self.width-134*2 + 17) * 0.5;
        leftAvatarView.centerY = self.middleY;
        leftAvatarView.avatarView.image = [HLUIHelper avatarPlaceholder];
        leftAvatarView.nickLabel.text = @"林深见鹿";
        [leftAvatarView.ageView setImage:[UIImage imageNamed:@"match_girl"] forState:UIControlStateNormal];
        [leftAvatarView.ageView setTitle:@"25" forState:UIControlStateNormal];
        [self addSubview:leftAvatarView];
        _leftAvatar = leftAvatarView;
        
        HLRtcMatchAvatarView *rightAvatarView = [HLRtcMatchAvatarView.alloc init];
        rightAvatarView.viewSize = CGSizeMake(134, 186);
        rightAvatarView.left = self.width;
        rightAvatarView.centerY = leftAvatarView.centerY;
        [rightAvatarView.avatarView sd_setImageWithURL:[NSURL URLWithString:HLRunTime.shareInstance.mine.avatar] placeholderImage:[HLUIHelper avatarPlaceholder]];
        rightAvatarView.nickLabel.text = HLRunTime.shareInstance.mine.nickName;
        [rightAvatarView.ageView setImage:[UIImage imageNamed:@"match_boy"] forState:UIControlStateNormal];
        [self addSubview:rightAvatarView];
    
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf startShowRightAvatar:rightAvatarView];
        });
        
        
        if (self.type == 2) {
            UIButton *chatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            chatBtn.viewSize = CGSizeMake(176, 44);
            chatBtn.bottom = self.height - 168;
            chatBtn.centerX = self.middleX;
            chatBtn.layer.cornerRadius = 22;
            chatBtn.layer.masksToBounds = YES;
            CAGradientLayer *gl = [CAGradientLayer layer];
            gl.frame = chatBtn.bounds;
            gl.startPoint = CGPointMake(1, 0.5);
            gl.endPoint = CGPointMake(0, 0.5);
            gl.colors = @[(__bridge id)[UIColor colorWithRed:252/255.0 green:101/255.0 blue:96/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:253/255.0 green:113/255.0 blue:202/255.0 alpha:1.0].CGColor];
            gl.locations = @[@(0), @(1.0f)];
            [chatBtn.layer addSublayer:gl];
            [chatBtn setTitle:@"开始聊天" forState:UIControlStateNormal];
            [chatBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            chatBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            [self addSubview:chatBtn];
            MJWeakSelf
            chatBtn.qmui_tapBlock = ^(__kindof UIControl *sender) {
                if (weakSelf.enterBlock) {
                    weakSelf.enterBlock();
                }
            };
        } else {
            _rematchButton = [UIButton buttonWithType:UIButtonTypeCustom];
            _rematchButton.hidden = YES;
            _rematchButton.viewSize = CGSizeMake(176, 44);
            _rematchButton.centerX = self.middleX;
            _rematchButton.layer.cornerRadius = 22;
            _rematchButton.layer.masksToBounds = YES;
            _rematchButton.bottom = self.height - 168;
            {
                CAGradientLayer *gl = [CAGradientLayer layer];
                gl.frame = CGRectMake(0,0,176,44);
                gl.startPoint = CGPointMake(1, 0.5);
                gl.endPoint = CGPointMake(0, 0.5);
                gl.colors = @[(__bridge id)[UIColor colorWithRed:252/255.0 green:101/255.0 blue:96/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:253/255.0 green:113/255.0 blue:202/255.0 alpha:1.0].CGColor];
                gl.locations = @[@(0), @(1.0f)];
                [_rematchButton.layer addSublayer:gl];
            }
            [_rematchButton setTitle:@"重新匹配" forState:UIControlStateNormal];
            _rematchButton.titleLabel.font = [UIFont systemFontOfSize:14];
            [self addSubview:_rematchButton];
            MJWeakSelf
            _rematchButton.qmui_tapBlock = ^(__kindof UIControl *sender) {
                if (weakSelf.rematchBlock) {
                    weakSelf.rematchBlock();
                }
            };
            
        }
    }
    return self;
}

- (void)startShowRightAvatar:(UIView *)view
{
    MJWeakSelf
    [UIView animateWithDuration:0.4 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:15 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        view.right = weakSelf.width - (weakSelf.width-134*2 + 17) * 0.5;
    } completion:^(BOOL finished) {
    }];
}

- (void)setMatchUserModel:(HLUserInfoModel *)matchUserModel
{
    _matchUserModel = matchUserModel;
    
    [_leftAvatar.avatarView sd_setImageWithURL:[NSURL URLWithString:matchUserModel.avatar] placeholderImage:[HLUIHelper avatarPlaceholder]];
    _leftAvatar.nickLabel.text = matchUserModel.nickName;
    [_leftAvatar.ageView setTitle:matchUserModel.age forState:UIControlStateNormal];
}


@end
